package com.lxy.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxy.common.R;
import com.lxy.constant.CodeConstant;
import com.lxy.constant.JwtClaimsConstant;
import com.lxy.dto.ShopLoginDTO;
import com.lxy.entity.Shop;
import com.lxy.properties.JwtProperties;
import com.lxy.service.ShopService;
import com.lxy.utils.JwtUtil;
import com.lxy.vo.AdminLoginVO;
import com.lxy.vo.ShopLoginVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private MailSender mailSender;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 发送email
     */
    public void sendEmail(String email,String title,String text){
        //给用户发消息
        SimpleMailMessage mailMessage = new SimpleMailMessage(); //创建一个邮件信息
        System.out.println("当前时间" + new Date());
        mailMessage.setSubject(title);//邮件的主题
        mailMessage.setText(text);//邮件的内容
        mailMessage.setTo(email);//接收者的邮箱
        mailMessage.setFrom("3319725459@qq.com");//发送者的邮箱
        mailSender.send(mailMessage);//发送邮件
    }


    /**
     * 店铺注册
     * @param shop
     * @return
     */
    @ApiOperation("")
    @PostMapping("/registration")
    public R registration(@RequestBody Shop shop){
        //判断营业执照是否重复
        String license = shop.getLicense();
        LambdaQueryWrapper<Shop> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shop::getLicense,license);
        Shop getByLicense = shopService.getOne(lambdaQueryWrapper);
        if(getByLicense!=null){
            //营业执照重复
            return R.error("该店铺已注册");
        }
        shop.setStatus(0);
        shop.setBanded(1);
        shop.setIsOperating(0);
        shopService.save(shop);
        //给申请人发信息
        String email = shop.getEmail();
        String title="您的申请已提交，将在七个工作日内完成审核";
        String text="感谢您对本软件的支持："+shop.getName()+"您申请的店铺："+shop.getShopName()+"将在审核通过后上架";
        sendEmail(email,title,text);
        return R.success("申请成功，将在七个工作日内完成审核");
    }

    /**
     * 店铺登录(目前只做邮箱发验证码登录)
     * @return
     */
    @ApiOperation("店铺登录(目前只做邮箱发验证码登录)")
    @PostMapping("/login")
    public R login(@RequestBody ShopLoginDTO shopLoginDTO){
        String email = shopLoginDTO.getEmail();
        //先判断是否审核通过
        LambdaQueryWrapper<Shop> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shop::getEmail,email);
        Shop shop = shopService.getOne(lambdaQueryWrapper);
        if(shop==null){
            //账户不存在
            return R.error("账户不存在，请先注册");
        }
        if(shop.getStatus()==0){
            return R.error("账号正在审核中");
        }
        if(shop.getBanded()==0){
            return R.error("账号被禁用，请联系管理员");
        }
        //验证验证码
        String key=email+ CodeConstant.SHOP_REGISTRATION;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer Rcode = (Integer)valueOperations.get(key);
        if(!Rcode.equals(shopLoginDTO.getCode())){
            return R.error("验证码错误，请重试");
        }
        //登录成功，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.SHOP_ID, shop.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getShopSecretKey(),
                jwtProperties.getShopTtl(),
                claims);
        ShopLoginVO shopLoginVO=new ShopLoginVO();
        shopLoginVO.setShop(shop);
        shopLoginVO.setToken(token);
        return R.success(shopLoginVO);
    }


}
